//
//  EventLogDetailsViewController.m
//  Seizure
//
//  Created by student on 3/29/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "EventLogDetailsViewController.h"
#import "EventLog.h"
#import "EventLocationAnnotation.h"
#import "MapKit/MKCircle.h"
#import "MapKit/MKPinAnnotationView.h"

@implementation EventLogDetailsViewController

@synthesize startDate;
@synthesize endDate;
@synthesize duration;
@synthesize location;
@synthesize mapView;

@synthesize eventDescribed;

//alpha value is for the opacity of the circle around the pin
static double ALPHA = 0.25;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil forEvent:(EventLog *)event
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if( self )
    {
        eventDescribed = event;
    }
    
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


#pragma mark - Custom Methods

- (NSString *)formatDuration:(NSTimeInterval)secondsElapsed
{
    //Get only whole seconds.
    int timeInSeconds = floor(secondsElapsed);
    
    //We assume that any event will not be longer than an hour.
    if(timeInSeconds >=  3600)
        return @"";
    
    int minutes = (timeInSeconds / 60);
    timeInSeconds = timeInSeconds % 60;
    
    int seconds = timeInSeconds;
    NSString *txtMinutes = nil;
    NSString *txtSeconds = nil;
    
    if (minutes == 1)
        txtMinutes = @"1 minute";
    else if (minutes > 1)
        txtMinutes = [NSString stringWithFormat:@"%d minutes", minutes];
    else 
        txtMinutes = @"";
    
    if (seconds == 1)
        txtSeconds = @"1 second";
    else if (seconds > 1)
        txtSeconds = [NSString stringWithFormat:@"%d seconds", seconds];
    else
        txtSeconds = @"";
    
    return [NSString stringWithFormat:@"Lasted %@ %@", txtMinutes, txtSeconds];
}

- (NSString *)formatLocation:(CLLocation *)loc
{
    return [NSString stringWithFormat:@"Location of Event: (%.2f°, %.2f°)\n Radius of Uncertainty: %.1f meters", loc.coordinate.latitude, loc.coordinate.longitude, loc.horizontalAccuracy];
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    startDate.text = [NSDateFormatter localizedStringFromDate:eventDescribed.startTime dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterLongStyle];
    endDate.text = [NSDateFormatter localizedStringFromDate:eventDescribed.endTime dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterLongStyle];
    duration.text = [self formatDuration:[eventDescribed.endTime timeIntervalSinceDate:eventDescribed.startTime]];
    location.text = [self formatLocation:eventDescribed.locationOfEvent];
    
    //Map pin location
    CLLocationCoordinate2D coord = eventDescribed.locationOfEvent.coordinate;
    EventLocationAnnotation *annotation = [[EventLocationAnnotation alloc]initWithCoordinate:coord];
    
    //Radius to highlight around the pin -- based on radius of uncertainty
    MKCircle *radius = [MKCircle circleWithCenterCoordinate:coord radius:eventDescribed.locationOfEvent.horizontalAccuracy];
    
    //Wideness of map that will be shown
    MKCoordinateRegion region;
    region.center = coord;

    //One degree of latitude is 111131.745 m.
    double latitudeDelta = (eventDescribed.locationOfEvent.horizontalAccuracy) / 111131.745;
    
    //Create a span based on the radius of the event.
    MKCoordinateSpan span = {latitudeDelta,0.01};
    region.span = span;
    
    
    [mapView addOverlay:radius];
    [mapView setRegion:region animated:YES];
    [mapView setMapType:MKMapTypeHybrid];
    [mapView addAnnotation:annotation];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -- Map View Delegate Methods

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay 
{
    MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay];
    circleView.fillColor = [UIColor redColor];
    circleView.alpha = ALPHA;
    return circleView;
}

- (MKAnnotationView *)mapView:(MKMapView *)mView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationIdentifier = @"Annotation";
    MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if (!pinView)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
        pinView.pinColor = MKPinAnnotationColorGreen;
        pinView.animatesDrop = YES;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}


@end
